<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Article;
use Markdown;
use Storage;
use Image;

class ArticlesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        // return view('index');
        $articles = Article::orderBy('created_at','asc')->paginate(2);
        return view('indexx', ['articles'=>$articles]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('add');
        
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'title'=>'required|max:50',
            'content'=>'required|max:1500',
            ]);

        // 方式写入
        $article = Article::create([
            'title'=>$request->title,
            'content'=>$request->content,
            ]);
        // dd($request->title);

        return redirect()->route('articles.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $article = Article::findOrFail($id);
        return view('show',['article'=>$article]);        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $article = Article::findOrFail($id);
        return view('edit',['article'=>$article]);        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->validate($request, [
            'title' => 'required|max:50',
            'content'=>'required|max:500',
        ]);

        $article = Article::findOrFail($id);
        $article->update([
            'title' => $request->title,
            'content' => $request->content,
        ]);

         return redirect()->route('articles.index');        
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $article = Article::findOrFail($id);
        $article->delete();
        return back();        
    }

    //显示markdown
      public function MKshow($id)
    {
        $article = Article::findOrFail($id);

        $article->content = Markdown::convertToHtml($article->content);

        return view('art.show',['article'=>$article]);
    }
    /**
     * 处理ajax markdown 阅览
     */
    public function markdown(Request $request)
    {
        return Markdown::convertToHtml($request->content);
    }


    public function  photo(Request $request)
    {
        // print_r($request->all());
        $prefix = "avatar".mt_rand(1,1000);
        $photo = $request->photo;
        $Origname = $photo->getClientOriginalName();
        $name = $prefix.$Origname;
        $src = "images/avatar/".$name;
        $realPath = $request->photo->getRealPath();
        Storage::disk('uploads')->put($src,file_get_contents($realPath));
        Image::make($photo)->save(public_path($src));
        return $src;
    }

    public function avater(Request $request)
    {
        return view('ajax');
    }    
}
